unit ActsDic;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, IBCustomDataset, Db, StdCtrls;

type
  TfmActsDic = class(TForm)
    DBGrid1: TDBGrid;
    DataSrc: TDataSource;
    btnSelect: TButton;
    btnAdd: TButton;
    btnEdit: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnSelectClick(Sender: TObject);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnAddClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
  private
    { Private declarations }
    FDataset: TIBDataset;
    FSelectedId: integer;
    PROCEDURE AddRec;
    procedure Select;
  public
    { Public declarations }
    property Dataset: TIBDataset read FDataset;
    property SelectedId: integer read FSelectedId;
  end;

var
  fmActsDic: TfmActsDic;

implementation
uses MainDataModule, ActsEdit;
{$R *.DFM}

procedure TfmActsDic.FormCreate(Sender: TObject);
begin
  FDataset := dmMain.CreateDataset(Self);
  FDataset.SelectSQL.Text := Format( 'select * from spis_get_acts( %d )',
                                     [ dmMain.FOtdel ] );
  FDataset.RefreshSQL.Text := 'select rnd.*, t.tipdoc ' +
                               'from regnewdoc rnd ' +
                               'join typedok t ' +
                                 'on t.kod = rnd.typedoc ' +
                              'where rnd.reg_n = :reg_n';
  DataSrc.DataSet := FDataset;
  //
  FDataset.InsertSQL.Text := 'INSERT INTO REGNEWDOC (REG_N, PERIOD, TYPEDOC, SOURCE_FIL, DEST_FIL, OTDEL_ID) ' +
      'VALUES (:REG_N, :PERIOD, :TYPEDOC, :SOURCE_FIL, :DEST_FIL, :OTDEL_ID)';
  FDataset.ModifySQL.Text := 'UPDATE REGNEWDOC SET REG_N = :REG_N, PERIOD = :PERIOD, TYPEDOC = :TYPEDOC, ' +
      'SOURCE_FIL = :SOURCE_FIL, DEST_FIL = :DEST_FIL, OTDEL_ID = :OTDEL_ID '+
      'WHERE REG_N = :OLD_REG_N ';
  FDataset.Open;
  FDataset.Last;
end;

procedure TfmActsDic.btnSelectClick(Sender: TObject);
begin
  Select;
end;

procedure TfmActsDic.Select;
begin
  FSelectedId := FDataset.FieldbyName('REG_N').AsInteger;
  ModalResult := mrOk;
  DBGrid1.SetFocus;
end;

procedure TfmActsDic.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = 13 then
    Select;
end;

procedure TfmActsDic.AddRec;
begin
  if fmActsEdit.Execute(True, FDataset) = mrOk then
    FdataSet.Refresh ;
end;

procedure TfmActsDic.btnAddClick(Sender: TObject);
begin
  AddRec;
end;

procedure TfmActsDic.btnEditClick(Sender: TObject);
begin
  if fmActsEdit.Execute(False, FDataset) = mrOk then
    FDataSet.Refresh;
end;

procedure TfmActsDic.DBGrid1DblClick(Sender: TObject);
begin
  Select;
end;

end.
